function parseJsonText(text) {
  try {
    return eval('(' + text + ')');
  } catch (e) {
    return {"status": "error", "message": "Parsing JSON failed in exception."}
  }
}

function createXmlHttpReq(handler) {
  var xmlhttp = null;
  if(window.XMLHttpRequest) { //Mozilla
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onload = handler;
    xmlhttp.onerror = handler;
  } else if (window.ActiveXObject) { // IE 
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      xmlhttp.onreadystatechange = handler;
    } catch (e) {
      try {  // for IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.onreadystatechange = handler;
      } catch (e) {}
    }
  }
  return xmlhttp;
}

function simpleCheckStatus(responseText) {
  var json_return = parseJsonText(responseText);
  if (json_return == null) {
    alert('Parsing response failed.');
    return;
  }
  if (json_return['status'] != 'OK') {
    alert('operation failed: ' + json_return['message']);
  } else {
    alert('Success.');  
  }
}

function encodeSelectElement(element, encode) {
  if (element.type.indexOf('select') != 0) {
    return '';
  } else {
    if (element.type == 'select-one') {
      return encode(element.name) + '=' +
             encode(element.options[element.selectedIndex].value);
    } else {
      var ret = '';
      var elementName = encode(element.name);
      var i, j, l, options = element.options;
      for (i = 0, l = options.length; i < l; ++i) {
        if (options[i].selected) {
          ret += elementName + '=' + encode(options[i].value) + '&';
        }
      }
      return (ret.length > 0) ? ret.substring(0, ret.length-1) : ret;
    }
  }
}

function encodeForm(form) {
  var ret = '';
  var first = true;
  var en = window.encodeURIComponent?encodeURIComponent:escape;
  for (var i = 0, l = form.elements.length; i < l; ++i) {
    var element = form.elements[i];
    var part = '';
    if (null == element.name || 0 == element.name.length
        || ' ' == element.name.charAt(0) || 'button' == element.type) {
      continue;
    }
    if (element.type == 'checkbox') {
      if (element.checked) {
        if (element.value) {
          part = en(element.name) + '=' + en(element.value);
        } else {
          part = en(element.name) + '=' + element.checked;
        }
      }
    } else if (element.type == 'radio') {
      if (element.checked) {
        part = en(element.name) + '=' + en(element.value);
      }
    } else if (element.type.indexOf('select') == 0) {
      part = encodeSelectElement(element, en);
    } else {
      part = en(element.name) + '=' + en(element.value);
    }
    if ('' == part)
      continue;
    if (first) {
      ret = part;
      first = false;
    } else {
      ret += '&' + part;
    }
  }
  return ret;
}

function ajaxUpdateForm(form, preupdate, callback) {
  if (eval(preupdate) == false)
    return;

  var node = document.createElement('input');
  node.setAttribute("type", "submit");
  node.setAttribute("name", "ajaxAction");
  node.setAttribute("style", "display: none;");
  node.style.width = 0;
  node.style.height = 0;
  form.appendChild(node);
  var req = createXmlHttpReq(
      function() {
	if (req.readyState != 4) {
	  return;
	}
	if (req.status != 200) {
	  return;
	}
	if (callback) {
	  var cb = callback + '("' + req.responseText + '")';
	  eval(cb);
	}
	delete req;
	req = null;
      });
  var reqString = encodeForm(form);
  reqString += '&tempT=' + new Date().getTime();
  req.open("POST", form.action, true);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  req.send(reqString);
  form.removeChild(node);
}

// Depreciated
var g_http_request = false;

function makeRequest(url, param, recallFunc) {
  g_http_request = false;

  if(window.XMLHttpRequest) { //Mozilla
    g_http_request = new XMLHttpRequest();
    if (g_http_request.overrideMimeType) { // set Mime type
      g_http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE 
    try {
      g_http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        g_http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!g_http_request) { 
    window.alert("Cannot create XMLHttpRequest!");
    return false;
  } else {
    g_http_request.onreadystatechange = recallFunc;
    g_http_request.open('GET', url+param, true);
    g_http_request.send(null);
  }
}

